<?php
    // Facebook External API
    // by Swen Kooij aka Kirk
    // swenkooij@gmail.com / info@thekirk.info
    
    // http://www.thekirk.info/

    class Facebook
    {
        public $Cookie;
        public $AccessToken;

        function __construct($username=null, $password=null, $cook='c')
		{
			if($username == null && $password == null && $cook != "c")
			{
				$this->Cookie = $cook;
				$this->getAccessToken();
			}
			else
			{
				$a = $this->cURL("https://login.facebook.com/login.php?login_attempt=1",true,null,"email=$username&pass=$password");
				preg_match('%Set-Cookie: ([^;]+);%',$a,$b);
				$c = $this->cURL("https://login.facebook.com/login.php?login_attempt=1",true,$b[1],"email=$username&pass=$password");
				preg_match_all('%Set-Cookie: ([^;]+);%',$c,$d);
				for($i=0;$i<count($d[0]);$i++)
				{
					$cookie.=$d[1][$i].";";
				}
				$this->Cookie = $cookie;
				$this->getAccessToken();
			}
        }
        
        function cURL($url, $header=NULL, $cookie=NULL, $p=NULL)
		{
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_HEADER, $header);
			curl_setopt($ch, CURLOPT_NOBODY, $header);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			if ($p) 
			{
				curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
				curl_setopt($ch, CURLOPT_POST, 1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
			}
			$result = curl_exec($ch);
			if ($result) 
			{
				return $result;
			} else 
			{
				return curl_error($ch);
			}
			curl_close($ch);
		}
        
        function isLogged()
        {
        	$data = $this->request("http://facebok.com/");
        	if (strlen(strstr($data,"login_form"))>0)
        	{
        		return false;
        	}
        	return true;
        }
        
        function request($url)
        {
            $data = cURL($url, null, $this->Cookie, null);
            return $data;
        }
        
        function api($p)
        {
            $data = $this->request("https://graph.facebook.com/" . $p);
            return json_decode($data);
        }
        
        function getAccessToken()
        {
            $data = $this->request("http://developers.facebook.com/docs/reference/api/");
            preg_match_all("~access_token=(.*)\"\>~", $data, $matches);
            $this->AccessToken = $matches[1][0];
            return $this->AccessToken;
        }
        
        function getObject($object)
        {
            $data = $this->request("https://graph.facebook.com/" . $object);
            return json_decode($data);
        }
        
        function getProfilePicture($username)
        {
            $data = $this->request("http://graph.facebook.com/" . $username . "/picture");
            return curl_getinfo($this->CurlObject, CURLINFO_EFFECTIVE_URL);
        }
        
        function getGroups($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/groups/?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getFriends($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/friends/?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getWallFeed($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/feed?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getNewsFeed($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/home?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getLinks($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/likes?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getMovies($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/movies?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getBooks($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/books?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
    
        function getMusic($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/music?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getPermissions($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/permissions?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getNotes($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/notes?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getPhotoTags($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/photos?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getPhotoAlbum($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/album?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getVideoTags($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/videos?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getVideoUploads($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/videos/uploaded?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getEvents($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/events?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getCheckins($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/checkins?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getLocations($person='me')
        {
            $data = $this->request("https://graph.facebook.com/$person/locations?access_token=" . $this->AccessToken);
            return json_decode($data)->data;
        }
        
        function getFullName()
        {
        	$data = $this->request("https://graph.facebook.com/me?access_token=" . $this->AccessToken);	
        	return json_decode($data)->name;
        }
        
        function isMemberOfGroup($groupname, $person='me')
        {
            $groups = $this->getGroups($person);
            foreach($groups as $group)
            {
                if($group->name == $groupname) return true;
            }
            return false;
        }
    }
?>